package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockListQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.asset.spare.v1.SpareStockListQueryResp;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/24
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class SpareStockApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final SpareStockApiService INSTANCE = new SpareStockApiService();
    }

    public static SpareStockApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private SpareStockApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private SpareStockApiGrpc.SpareStockApiBlockingStub getStub(ManagedChannel channel) {
        return SpareStockApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 库存管理列表查询
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SpareStockListQueryResp> querySpareStockList(
            String warehouseId,
            CallBack<SpareStockListQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, SpareStockListQueryResp>(callBack) {
            @Override
            protected SpareStockListQueryResp doRequestData(ManagedChannel channel) {
                SpareStockListQueryReq message = SpareStockListQueryReq.newBuilder()
                        .setComQuery(ComQuery.newBuilder().setPage(1).setPageSize(100000).build())
                        .setTenantNo(1)
                        .addWarehouseId(warehouseId)
                        .build();
                Logger.d(message);
                SpareStockListQueryResp response = null;
                try {
                    response = getStub(channel).querySpareStockList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "querySpareStockList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 库存管理列表查询
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, SpareStockListQueryResp> querySpareStockList(
            String keyword,
            String warehouseId,
            CallBack<SpareStockListQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, SpareStockListQueryResp>(callBack) {
            @Override
            protected SpareStockListQueryResp doRequestData(ManagedChannel channel) {
                SpareStockListQueryReq message = SpareStockListQueryReq.newBuilder()
                        .setComQuery(ComQuery.newBuilder().setPage(1).setPageSize(100000).build())
                        .setKeyword(keyword)
                        .setTenantNo(1)
                        .addWarehouseId(warehouseId)
                        .build();
                Logger.d(message);
                SpareStockListQueryResp response = null;
                try {
                    response = getStub(channel).querySpareStockList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "querySpareStockList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }
}
